package com.jincp.filepicker.base;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;

import com.jincp.common.model.ToolBarOptions;
import com.jincp.common.utils.StatusBarUtil;
import com.jincp.filepicker.LoadingDialogFragment;

/**
 * =========================================================
 * @ProjectName:
 * @ClassName: FileBaseActivity
 * @Description:
 * @Author: jincp
 * @CreateDate: 2019/3/22 21:21
 * =========================================================
 */
public abstract class FileBaseActivity extends AppCompatActivity implements View.OnClickListener{

    /**
     * 加载对话框
     */
    public LoadingDialogFragment dialogLoading;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        onBeforeSetContentLayout();
        configIntentValue();
        if (getContentLayout() != 0) {
            setContentView(getContentLayout());
        }
        initViews();
        initData(savedInstanceState);
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        setIntent(intent);
        configIntentValue();
    }

    protected void configIntentValue() {
    }

    protected void onBeforeSetContentLayout() {
        StatusBarUtil.setLightMode(this);
    }

    protected abstract int getContentLayout();

    protected abstract void initViews();

    protected abstract void initData(Bundle savedInstanceState);

    public void setCustomToolBar(Toolbar toolBar, ToolBarOptions options) {
        if (toolBar == null || options == null) return;
        if (!TextUtils.isEmpty(options.titleString)) {
            toolBar.setTitle(options.titleString);
        }
        if (!TextUtils.isEmpty(options.titleString)) {
            toolBar.setTitle(options.titleString);
        }
        if (options.logoId != 0) {
            toolBar.setLogo(options.logoId);
        }
        setSupportActionBar(toolBar);

        if (options.navigateId != 0) {
            toolBar.setNavigationIcon(options.navigateId);
            toolBar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onBackPressed();
                }
            });
        }
    }

    @Override
    public void onClick(View view) {

    }

    /**
     * 显示加载对话框
     *
     * @param message 文字显示信息
     */
    public void showWaitDialog(String message) {
        try {
            hideWaitDialog();
            if (dialogLoading == null) {
                dialogLoading = new LoadingDialogFragment.Builder()
                        .setAnimation(0)
                        .setMessage(message)
                        .build();
            }
            dialogLoading.show(getSupportFragmentManager(), "LoadDialogFragment");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 隐藏加载对话框
     */
    public void hideWaitDialog() {
        if (dialogLoading != null) {
            try {
                dialogLoading.dismissAllowingStateLoss();
                dialogLoading = null;
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}
